
const { createDuty, updateByEmpId, queryAll } = require('../service/duty.service')


class DutyController {
    async create(ctx) {
        const duty = {
            empId: ctx.state.emp.id,
            empName: ctx.state.emp.empName,
            signInTime: Date.now()
        }
        const res = await createDuty(duty)
        if (res) {
            ctx.body = {
                code: '200',
                message: '签到成功',
                data: res
            }
        } else {
            ctx.body = {
                code: '200',
                message: '今日已签到'
            }
        }

    }

    async update(ctx) {
        console.log(ctx.state.emp.id);
        const duty = {
            empId: ctx.state.emp.id,
            signOutTime: Date.now()
        }
        const res = await updateByEmpId(duty)
        ctx.body = {
            code: '200',
            message: res,
        }
    }

    async query(ctx) {
        //今天的时间
        var start = new Date();
        start.setTime(start.getTime());
        var startTime = start.getFullYear() + "-" + (start.getMonth() + 1) + "-" + start.getDate();
        console.log(startTime);
        //明天的时间
        var end = new Date();
        end.setTime(end.getTime() + 24 * 60 * 60 * 1000);
        var endTime = end.getFullYear() + "-" + (end.getMonth() + 1) + "-" + end.getDate();

        const { pageNum = 1, pageSize = 5 ,empName} = ctx.request.query


        const whereOpt = { startTime, endTime, empName }
        const res = await queryAll(pageNum, pageSize, whereOpt)
        ctx.body = {
            code: '200',
            message: '今日签到列表获取成功',
            data: res
        }
    }
    async queryHistory(ctx) {
        const { pageNum = 1, pageSize = 5,...whereOpt } = ctx.request.query

        const res = await queryAll(pageNum, pageSize, whereOpt)
        ctx.body = {
            code: '200',
            message: '签到列表获取成功',
            data: res
        }
    }
}

module.exports = new DutyController()